package cst.a228htt.startproject228;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import cst.a228htt.startproject228.bean.WeatherInfo;
import cst.a228htt.startproject228.listener.WeatherListener;
import cst.a228htt.startproject228.model.Weather51Model;
import cst.a228htt.startproject228.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener{

    private EditText cityNoInput;
    private TextView city;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cityNoInput=(EditText)findViewById(R.id.ed_city_no);
        city=(TextView)findViewById(R.id.City);
        temp=(TextView)findViewById(R.id.temp);
        wd=(TextView)findViewById(R.id.WD);
        ws=(TextView)findViewById(R.id.WS);
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.button:
            WeatherModel weatherModel = new WeatherModel();
            weatherModel.getWeather(cityNoInput.getText().toString(),this);
            break;
        case R.id.button2:
            Weather51Model weather51Model = new Weather51Model();
            weather51Model.getWeather(cityNoInput.getText().toString(),this);
            break;
        default:
            break;

    }
    }

    @Override
    public void onResponse(WeatherInfo weather) {
        if (weather != null){
            city.setText(weather.getCity());
            temp.setText(weather.getTemp());
            wd.setText(weather.getWD());
            ws.setText(weather.getWS());
        }
        else {
            city.setText("未知");
        }

    }

    @Override
    public void onFail(String msg) {
        city.setText(msg);
     }
}
